package com.chinemit.cui.components.container.group
{
	import com.chinemit.cui.components.itemrenderer.ItemRendererBase;
	import com.chinemit.cui.components.navigation.tab.TabBar;
	import com.chinemit.cui.events.SelectEvent;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ItemMapConponent extends DataGroup{
		
		public static const DIR_HORIZONTAL:String="directionHorizontal";
		public static const DIR_VERTAICAL:String="directionVertical";
		
		private var _pageItemRow:int;     //每页行数
		private var _pageItemColumn:int;  //每页列数
		
		public var pageSum:int; //总页数
		
		private var pageItemSum:int;//每页Item总数
		
		private var _currentPageIndex:int; //当前页索引
		
		private var pageDataList:Array;//页面数据列表
		
		private var _pageIndexBar:TabBar;//
		
		private var Setp:int; //单步步长,不采用翻页模式时候
		
		private var isSwitchingComplete:Boolean = true;
		
		private var switchingSpeed:Number = 0.6; //切换速度
		
		private var _switchDirection:String = ""; //切换方向
		
		private var isAuto:Boolean = false;  //自动播放
		private var tick:int = 1000; // 自动播放间隔时间
		
		public var fillDefect:Boolean = false;
		
		/**
		 * Game Map Conponent 
		 * @param _dataProvider 数据源
		 * @param _itemRender Tile渲染器
		 * @param _w 宽度
		 * @param _h 高度
		 * @param _row 每页呈现行数   
		 * @param _column 每页呈现列数
		 * @param _hGap 水平方向间隔
		 * @param _vGap 垂直方向间隔
		 * @param _startIndex 起始索引（注：索引从零开始计算）
		 * @param _switchingSpeed 切换速度
		 * 
		 */		
		public function ItemMapConponent(_w:Number,
										 _h:Number,
										 _itemRender:Class,
										 _dataProvider:Array = null,
										 _row:int=1,
										 _column:int=1,
										 _hGap:Number=15,
										 _vGap:Number=15,
										 _startIndex:int=0,
										 _switchingSpeed:Number = 0.6,
										 _isFill:Boolean = false){
			
			fillDefect = _isFill;
			
			//初始化切换方向
			switchDirection = DIR_HORIZONTAL;
			//设置每页行数,列数
			_pageItemRow = _row;
			_pageItemColumn = _column;
			//设置纵向间隔和垂直间隔
			_horizontalGap = _hGap;
			_verticalGap = _vGap;
			// 设置Tile宽度,高度
//			tileWidth = ((_w-((this.pageItemColumn-1)*this._horizontalGap))/this.pageItemColumn);
//			tileHeight = ((_h-((this.pageItemRow-1)*this._verticalGap))/this.pageItemRow);
			// 设置当前索引
			_currentPageIndex = _startIndex;
			// 设置切换速度
			switchingSpeed = _switchingSpeed;
			
			tileWidth = ((_w-((this.pageItemColumn-1)*this._horizontalGap))/this.pageItemColumn);
			tileHeight = ((_h-((this.pageItemRow-1)*this._verticalGap))/this.pageItemRow);
			super(_dataProvider,_itemRender,_w,_h);
			
		}
		
		public function get switchDirection():String
		{
			return _switchDirection;
		}

		public function set switchDirection(value:String):void
		{
			_switchDirection = value;
		}

		public function get pageItemColumn():int
		{
			return _pageItemColumn;
		}

		public function set pageItemColumn(value:int):void
		{
			_pageItemColumn = value;
		}

		public function get pageItemRow():int
		{
			return _pageItemRow;
		}

		public function set pageItemRow(value:int):void
		{
			_pageItemRow = value;
//			tileWidth = ((this.canvasWidth-((this.pageItemColumn-1)*this._horizontalGap))/this.pageItemColumn);
//			tileHeight = ((this.canvasHeight-((this.pageItemRow-1)*this._verticalGap))/this.pageItemRow);
		}

		override public function set data(value:Array):void{
			_data = value;
			if(itemRenderClass){
				initPage();
			}
		}

		public function get currentPageIndex():int
		{
			return _currentPageIndex;
		}

		public function set currentPageIndex(value:int):void
		{
			_currentPageIndex = value;
			//设置索引工具条当前索引
			if(_pageIndexBar){
				_pageIndexBar.currentIndex = value;
			}
		}

		public function get pageIndexBar():TabBar{
			return _pageIndexBar;
		}

		public function set pageIndexBar(value:TabBar):void{
			if(_pageIndexBar){
				pageIndexBar.removeEventListener(SelectEvent.EVENT_SELECT,selectEventHandler);
			}
			if(value){
				_pageIndexBar = value;
				_pageIndexBar.init(pageSum ,currentPageIndex);
				_pageIndexBar.addEventListener(SelectEvent.EVENT_SELECT,selectEventHandler);
			}
		}
		/**
		 * 初始化页面 
		 * 
		 */		
		private function initPage():void{
			
			if(!data){
				return;
			}
//			if(data.length==0){
//				return;
//			}
			//重构界面
			if(pageItemRow!=0 && pageItemColumn==0){
				pageItemColumn = 1;
			}else if(pageItemRow==0 && pageItemColumn!=0){
				pageItemRow = 1;
			}else if(pageItemRow==0 && pageItemColumn ==0){
				pageItemRow = 1;
				pageItemColumn = 1;
			}
			
			pageItemSum = pageItemRow*pageItemColumn
			
			var tmpPageSum:Number = (data.length)/pageItemSum;
			tmpPageSum>(int(tmpPageSum))?pageSum = int(tmpPageSum)+1:pageSum = int(tmpPageSum);
			
			pageDataList = [];
			for(var i:int=0;i<data.length;i+=pageItemSum){
				pageDataList.push(data.slice(i,i+pageItemSum));
			}
			//清除当前界面
			if(currentPage){
				while(currentPage.numChildren!=0){
					currentPage.removeChildAt(currentPage.numChildren-1);
				}
				currentPage = null;
				if(_pageIndexBar){
					_pageIndexBar.init(pageSum,0);
				}
				currentPageIndex = 0
			}
			if(currentPageIndex>=pageSum ){
				currentPageIndex = pageSum-1;
				currentPageIndex>=0?currentPageIndex = currentPageIndex:currentPageIndex=0;
			}
			var tmpFirstPageData:Array = pageDataList[currentPageIndex];
		 	var page:Sprite = initTileItem(tmpFirstPageData);
			this.addChild(page);
			page.x = containerMask.x;
			page.y = containerMask.y;
			//设置当前页面
			currentPage = page;
		}
		
		/**
		 * 初始化 Tile Item 
		 * @return 
		 * 
		 */		
		private function initTileItem(arr:Array):Sprite{
			
			//末页自适应时使用
			
//			if(arr.length == this.pageItemColumn*this.pageItemRow){
			    //当前页与目标相符
				tileWidth = ((this.canvasWidth-((this.pageItemColumn-1)*this._horizontalGap))/this.pageItemColumn);
				tileHeight = ((this.canvasHeight-((this.pageItemRow-1)*this._verticalGap))/this.pageItemRow);
//			}else if(arr.length<this.pageItemColumn){
//			    //当前页项目数少于列数
//				tileWidth = ((this.canvasWidth-((arr.length-1)*this._horizontalGap))/arr.length);
//				tileHeight = this.canvasHeight;
//			}
			
			if(!arr){
				arr = [];
			}
			
			var pageComtainer:Sprite = new Sprite;
			pageComtainer.graphics.beginFill(0xffffff,0.0);
			pageComtainer.graphics.drawRect(0,0,canvasWidth,canvasHeight);
			pageComtainer.graphics.endFill();
			
			var tmpRow:int = 0;
			var tmpColumn:int = 0;
			var len:int = arr.length;
			
			if(fillDefect){
				len = pageItemSum;
			}
			
			for(var i:int=0;i<len;i++){
				tmpColumn = i%pageItemColumn;
				if(tmpColumn==0 && i!=0){
					tmpRow++
				}
				
				var tmpChild:*;
				if((arr.length-1)<=len){
					tmpChild= new itemRenderClass(arr[i],tileWidth,tileHeight)
				}else{
					tmpChild= new itemRenderClass(null,tileWidth,tileHeight)
				}
				(tmpChild as DisplayObject).addEventListener(MouseEvent.CLICK,selectItemEventHandler);
				tmpChild.x = tmpColumn*tileWidth+tmpColumn*_verticalGap;
				tmpChild.y = tmpRow*tileHeight+tmpRow*_horizontalGap;
				pageComtainer.addChild(tmpChild);
			}
			return pageComtainer;
		}
		
		/**
		 * 
		 * @param event
		 * 
		 */		
		private function selectItemEventHandler(event:MouseEvent):void{
			this.dispatchEvent(new SelectEvent(SelectEvent.EVENT_SELECT,false,false,0,event.target));
		}
		
		/**
		 * 切换页面
		 * @param oldIndex
		 * @param newIndex
		 * 
		 */		
		private function switchingPage(oldIndex:int,newIndex:int):void{
			
			isSwitchingComplete = false;
			var page:Sprite = initTileItem(pageDataList[newIndex]);
			this.addChild(page);
			currentPage.cacheAsBitmap = true;
			page.cacheAsBitmap = true;
			page.visible = true;
			
			var toX:Number =  containerMask.x;
			var toY:Number = containerMask.y;
			if(this.switchDirection == DIR_HORIZONTAL){
				if(oldIndex > newIndex){
					page.x = -containerMask.width-_horizontalGap;
					TweenLite.to(currentPage,switchingSpeed,{x:this.containerMask.width+_horizontalGap});
				}else{
					page.x = containerMask.width+_horizontalGap;
					TweenLite.to(currentPage,switchingSpeed,{x:-this.containerMask.width-_horizontalGap});
				}
			}else{
				if(oldIndex > newIndex){
					page.y = -containerMask.height-_horizontalGap;
					TweenLite.to(currentPage,switchingSpeed,{y:this.containerMask.height+_horizontalGap});
				}else{
					page.y = containerMask.height+_horizontalGap;
					TweenLite.to(currentPage,switchingSpeed,{y:-this.containerMask.height-_horizontalGap});
				}
			}
			TweenLite.to(page,switchingSpeed,{x:toX,y:toY,onComplete:onPageChangeComplete,onCompleteParams:[currentPage]});
			//
			currentPage = page;
		}
		
		/**
		 * 当页面切换完毕 
		 * @param page
		 * 
		 */		
		private function onPageChangeComplete(page:Sprite):void{
			currentPage.cacheAsBitmap = false;
			//dispose page
			page.visible = false;
			while(page.numChildren!=0){
				page.removeChildAt(page.numChildren-1);
			}
			this.removeChild(page);
			page = null;
			//
			isSwitchingComplete = true;
		}
		
		/**
		 * 上一页 
		 * 
		 */		
		public function prePage():void{
			if(!isSwitchingComplete){
				return;
			}
			if(currentPageIndex>0){
				var oldIndex:int = currentPageIndex;
				var newIndex:int = currentPageIndex-1;
				switchingPage(oldIndex,newIndex);
				currentPageIndex--;
			}
		}
		
		/**
		 * 下一页 
		 * 
		 */		
		public function nextPage():void{
			if(!isSwitchingComplete){
				return;
			}
			if(currentPageIndex<pageSum-1){
				var oldIndex:int = currentPageIndex;
				var newIndex:int = currentPageIndex+1;
				switchingPage(oldIndex,newIndex);
				currentPageIndex++;
			}
		}
		
		/**
		 * 根据索引跳转页面 
		 * @param index //跳转页数
		 * 
		 */		
		public function gotoPageByIndex(index:int):void{
			if(this.currentPageIndex == index){return;}
			if(!isSwitchingComplete){
				pageIndexBar.currentIndex = currentPageIndex;
				return;
			}
			var oldIndex:int = currentPageIndex;
			var newIndex:int = index>(pageDataList.length-1)?(pageDataList.length-1):index;
			switchingPage(oldIndex,newIndex);
			currentPageIndex = newIndex;
		}
		
		/**
		 * 页面索引控制器 
		 * @param event
		 * 
		 */		
		protected function selectEventHandler(event:SelectEvent):void{
			gotoPageByIndex(event.index);
		}
	}
}